/***************************************************************************
创建者：华磊
 开始时间：               : 2020.12.01
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************
*   电机状态窗口程序
*                                                                         *
 ***************************************************************************/
#include "motorstatusview.h"
#include "ui_motorstatusview.h"

MotorStatusView::MotorStatusView(int styleId,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MotorStatusView)
{
    ui->setupUi(this);
    graphFlag=1;

    minYAxis = -10;
    maxYAxis = 10;
    move(0,0);
    initialDisplayStyle(styleId);
    initGraphView();
}

void MotorStatusView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        setGeometry(0,0,600,450);
        ui->customPlot->setGeometry(15,260,430,150);

        ui->data->setGeometry(20,410,90,17);
        ui->data->setParent(this);
        ui->horizontalSlider->setGeometry(110,410,140,30);
        ui->horizontalSlider->setParent(this);
        ui->pushButton->setGeometry(255,410,70,30);
        ui->pushButton->setParent(this);
        ui->pushButton_2->setGeometry(315,410,70,30);
        ui->pushButton_2->setParent(this);
        ui->pushButton_3->setGeometry(520,410,65,30);
        ui->pushButton_3->setParent(this);
        break;
    default:
        break;
    }
}

MotorStatusView::~MotorStatusView()
{

}

void MotorStatusView::initGraphView()
{
    ui->position_comboBox->addItem(tr("无"));
    ui->velocity_comboBox->addItem(tr("无"));
    ui->torque_comboBox->addItem(tr("无"));
    ui->power_comboBox->addItem(tr("无"));
    ui->followoffset_comboBox->addItem(tr("无"));

    //setStyleSheet("background-color:white;");
    ui->backwidget->setStyleSheet("background-color:white;");

    rubberBand = new QRubberBand(QRubberBand::Rectangle, ui->customPlot);
    ui->data->setText("y:");


    ui->customPlot->addGraph();
    ui->customPlot->graph(0)->setPen(QPen(Qt::blue));
    ui->customPlot->addGraph();
    ui->customPlot->graph(1)->setPen(QPen(Qt::red));
    ui->customPlot->addGraph();
    ui->customPlot->graph(2)->setPen(QPen(Qt::green));
    ui->customPlot->addGraph();
    ui->customPlot->graph(3)->setPen(QPen(Qt::black));
    ui->customPlot->addGraph();
    ui->customPlot->graph(4)->setPen(QPen(QColor(255, 162, 0)));
    ui->customPlot->addGraph();
    ui->customPlot->graph(5)->setPen(QPen(Qt::white));

    ui->customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);
    ui->customPlot->xAxis->setDateTimeFormat("hh:mm:ss");
    ui->customPlot->xAxis->setAutoTickStep(false);
    ui->customPlot->xAxis->setTickStep(2);
    ui->customPlot->axisRect()->setupFullAxesBox();

    ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
    connect(ui->customPlot, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePress(QMouseEvent*)));
    connect(ui->customPlot, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(mouseMove(QMouseEvent*)));
    connect(ui->customPlot, SIGNAL(mouseRelease(QMouseEvent*)), this, SLOT(mouseRelease(QMouseEvent*)));
    connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange)));
    connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange)));

    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    model = new QStandardItemModel();
    ui->tableView->setModel(model);
    model->setColumnCount(11);
    ui->tableView->setColumnWidth(0,40);
    ui->tableView->setColumnWidth(1,65);
    ui->tableView->setColumnWidth(2,50);
    ui->tableView->setColumnWidth(3,65);
    ui->tableView->setColumnWidth(4,65);
    ui->tableView->setColumnWidth(5,65);
    ui->tableView->setColumnWidth(6,95);
    ui->tableView->setColumnWidth(7,95);
    ui->tableView->setColumnWidth(8,95);
    ui->tableView->setColumnWidth(9,95);
    ui->tableView->setColumnWidth(10,95);
    ui->tableView->verticalHeader()->hide();
    QHeaderView *headerView = ui->tableView->horizontalHeader();
//    headerView->setResizeMode(QHeaderView::Fixed);
    model->setHeaderData(0,Qt::Horizontal,tr("轴"));
    model->setHeaderData(1,Qt::Horizontal,tr("位置"));
    model->setHeaderData(2,Qt::Horizontal,tr("速度"));
    model->setHeaderData(3,Qt::Horizontal,tr("力距"));
    model->setHeaderData(4,Qt::Horizontal,tr("误差"));
    model->setHeaderData(5,Qt::Horizontal,tr("功率"));
    model->setHeaderData(6,Qt::Horizontal,tr("位置最大值"));
    model->setHeaderData(7,Qt::Horizontal,tr("速度最大值"));
    model->setHeaderData(8,Qt::Horizontal,tr("力距最大值"));
    model->setHeaderData(9,Qt::Horizontal,tr("误差最大值"));
    model->setHeaderData(10,Qt::Horizontal,tr("功率最大值"));

    for(int i = 0 ;i<D_ROBOT_AXIS_MAX;i++)
    {
        model->setItem(i,0,new QStandardItem(QString::number(i+1)));
        model->setItem(i,1,new QStandardItem(""));
        model->setItem(i,2,new QStandardItem(""));
        model->setItem(i,3,new QStandardItem(""));
        model->setItem(i,4,new QStandardItem(""));
        model->setItem(i,5,new QStandardItem(""));
        model->setItem(i,6,new QStandardItem(""));
        model->setItem(i,7,new QStandardItem(""));
        model->setItem(i,8,new QStandardItem(""));
        model->setItem(i,9,new QStandardItem(""));
        model->setItem(i,10,new QStandardItem(""));
    }
}

void MotorStatusView::mousePress(QMouseEvent* mevent)
{
    if(mevent->button() == Qt::RightButton)
    {
        rubberOrigin = mevent->pos();
        rubberBand->setGeometry(QRect(rubberOrigin, QSize()));
        rubberBand->show();
    }
    else if(mevent->button() == Qt::LeftButton)
    {
        double pos = ui->customPlot->yAxis->pixelToCoord(mevent->pos().y());
        QString s=QString::number(pos);
        ui->data->setText("y:"+s);
    }
    else
    {

    }
}

void MotorStatusView::mouseMove(QMouseEvent *mevent)
{
    if(rubberBand->isVisible())
    {
        rubberBand->setGeometry(QRect(rubberOrigin, mevent->pos()).normalized());
    }
    else
    {

    }
}

void MotorStatusView::mouseRelease(QMouseEvent *mevent)
{
    Q_UNUSED(mevent);
    if (rubberBand->isVisible())
    {
        const QRect zoomRect = rubberBand->geometry();
        int xp1, yp1, xp2, yp2;
        zoomRect.getCoords(&xp1, &yp1, &xp2, &yp2);
        double x1 = ui->customPlot->xAxis->pixelToCoord(xp1);
        double x2 = ui->customPlot->xAxis->pixelToCoord(xp2);
        double y1 = ui->customPlot->yAxis->pixelToCoord(yp1);
        double y2 = ui->customPlot->yAxis->pixelToCoord(yp2);

        ui->customPlot->xAxis->setRange(x1, x2);
        ui->customPlot->yAxis->setRange(y1, y2);

        rubberBand->hide();
        ui->customPlot->replot();
    }
    else
    {

    }
}


void MotorStatusView::updateMotorStatusView()
{
    emit showMotorStatus();
}

void MotorStatusView::updateView()
{
    for(int n = 0;n<6;n++)//for(int n=0;n<axisCount;n++)
    {
        if(n < axisCount)
        {
            //QString s=QString::number(n);
            QString s1 = QString::number(value0[n],'d',2);
            QString s2 = QString::number(value1[n],'d',2);
            QString s3 = QString::number(value2[n],'d',2);
            QString s4 = QString::number(value3[n],'d',2);
            QString s5 = QString::number(value4[n],'d',2);
            QString s6 = QString::number(valueMax0[n],'d',3);//QString::number(value5[n]);
            QString s7 = QString::number(valueMax1[n],'d',3);//QString::number(value6[n]);
            QString s8 = QString::number(valueMax2[n],'d',3);//QString::number(value7[n]);
            QString s9 = QString::number(valueMax3[n],'d',3);//QString::number(value8[n]);
            QString s10 = QString::number(valueMax4[n],'d',3);//QString::number(value9[n]);

            //model->item(n,0)->setText(s);
            if(model->item(n,1)->text() != s1)
            {
                model->item(n,1)->setText(s1);
            }
            if(model->item(n,2)->text() != s2)
            {
                model->item(n,2)->setText(s2);
            }
            if(model->item(n,3)->text() != s3)
            {
                model->item(n,3)->setText(s3);
            }
            if(model->item(n,4)->text() != s4)
            {
                model->item(n,4)->setText(s4);
            }
            if(model->item(n,5)->text() != s5)
            {
                model->item(n,5)->setText(s5);
            }
            if(model->item(n,6)->text() != s6)
            {
                model->item(n,6)->setText(s6);
            }
            if(model->item(n,7)->text() != s7)
            {
                model->item(n,7)->setText(s7);
            }
            if(model->item(n,8)->text() != s8)
            {
                model->item(n,8)->setText(s8);
            }
            if(model->item(n,9)->text() != s9)
            {
                model->item(n,9)->setText(s9);
            }
            if(model->item(n,10)->text() != s10)
            {
                model->item(n,10)->setText(s10);
            }
        }
    }
}

void MotorStatusView::on_pushButton_clicked()
{
    emit startWaveForm();
    ui->horizontalSlider->setValue(5);
    ui->horizontalSlider->setEnabled(0);
}

void MotorStatusView::on_pushButton_2_clicked()
{
    emit ceaseWaveForm();
    ui->horizontalSlider->setEnabled(1);
}

void MotorStatusView::on_pushButton_3_clicked()
{
    emit ceaseWaveForm();
    this->hide();
}

void MotorStatusView::update()
{
    double key = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0;//当前时间
    mainKey = key;

    if(graphFlag==1)
    {
        for(int x=1;x<axisCount+1;x++)
        {
            QString l=QString::number(x);
            ui->position_comboBox->addItem(l);
            ui->velocity_comboBox->addItem(l);
            ui->torque_comboBox->addItem(l);
            ui->power_comboBox->addItem(l);
            ui->followoffset_comboBox->addItem(l);
        }
        graphFlag=0;
    }

    for(int n=0;n<axisCount+2;n++)
    {
        if(ui->position_comboBox->currentIndex()==n)
        {
            if(n==0)
            {}
            else
            {
                ui->customPlot->graph(0)->addData(key, value0[n-1]);
            }
        }

        if(ui->velocity_comboBox->currentIndex()==n)
        {
            if(n==0)
            {}
            else
            {
                ui->customPlot->graph(1)->addData(key, value1[n-1]);
            }
        }

        if(ui->torque_comboBox->currentIndex()==n)
        {
            if(n==0)
            {}
            else
            {
                ui->customPlot->graph(2)->addData(key, value2[n-1]);
            }
        }

        if(ui->followoffset_comboBox->currentIndex()==n)
        {
            if(n==0)
            {}
            else
            {
                ui->customPlot->graph(3)->addData(key, value3[n-1]);
            }
        }

        if(ui->power_comboBox->currentIndex()==n)
        {
            if(n==0)
            {}
            else
            {
                ui->customPlot->graph(4)->addData(key, value4[n-1]);
            }
        }
    }

    for(int g=0;g<5;g++)
    {
        ui->customPlot->graph(g)->removeDataBefore(key-32);//保留长度(当前时间-32) 即为长度32,若注释,数据一直保留
        ui->customPlot->graph(g)->rescaleValueAxis();
    }
    ui->customPlot->xAxis->setRange(key+1.25,8,Qt::AlignRight);//界面显示多少数据(中间,8秒)
    ui->customPlot->replot();
}

void MotorStatusView::changeLanguage()
{
    ui->retranslateUi(this);
    setViewText();
}

void MotorStatusView::setViewText()
{
    ui->position_comboBox->setItemText(0, tr("无"));
    ui->velocity_comboBox->setItemText(0, tr("无"));
    ui->torque_comboBox->setItemText(0, tr("无"));
    ui->power_comboBox->setItemText(0, tr("无"));
    ui->followoffset_comboBox->setItemText(0, tr("无"));

    model->setHeaderData(0,Qt::Horizontal,tr("轴"));
    model->setHeaderData(1,Qt::Horizontal,tr("位置"));
    model->setHeaderData(2,Qt::Horizontal,tr("速度"));
    model->setHeaderData(3,Qt::Horizontal,tr("力距"));
    model->setHeaderData(4,Qt::Horizontal,tr("误差"));
    model->setHeaderData(5,Qt::Horizontal,tr("功率"));
    model->setHeaderData(6,Qt::Horizontal,tr("位置最大值"));
    model->setHeaderData(7,Qt::Horizontal,tr("速度最大值"));
    model->setHeaderData(8,Qt::Horizontal,tr("力距最大值"));
    model->setHeaderData(9,Qt::Horizontal,tr("误差最大值"));
    model->setHeaderData(10,Qt::Horizontal,tr("功率最大值"));
}

void MotorStatusView::on_horizontalSlider_sliderMoved(int position)
{
    ui->customPlot->xAxis->setRange(mainKey-position-3,mainKey+position-3);
    ui->customPlot->yAxis->setRange(minYAxis-position,maxYAxis+position);
    ui->customPlot->replot();
}
